package com.yuanfeng.userms.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanfeng.commoms.config.YFConfig;
import com.yuanfeng.commoms.constant.CommonType;
import com.yuanfeng.commoms.dto.userms.UserInfoDTO;
import com.yuanfeng.commoms.exception.BizCodeEnume;
import com.yuanfeng.commoms.util.ParseTokenUtils;
import com.yuanfeng.commoms.util.RedisUtil;
import com.yuanfeng.commoms.util.ResponseResult;
import com.yuanfeng.commoms.util.querypage.PageUtils;
import com.yuanfeng.commoms.util.querypage.Query;
import com.yuanfeng.commoms.vo.user.UserInfoVO;
import com.yuanfeng.userms.dto.UserPlatformMessageDTO;
import com.yuanfeng.userms.entity.UserPlatformMessageEntity;
import com.yuanfeng.userms.mapper.UserPlatformMessageMapper;
import com.yuanfeng.userms.service.UserPlatformMessageService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 *
 */
@Service
public class UserPlatformMessageServiceImpl extends ServiceImpl<UserPlatformMessageMapper, UserPlatformMessageEntity>
    implements UserPlatformMessageService{

    @Override
    public ResponseResult sendPlatformMessage(Map<String, Object> param) {
        if (null == param.get("token") || StringUtils.isEmpty((String) param.get("token"))) {
            return ResponseResult.fail(BizCodeEnume.TOKEN_IS_NULL);
        }
        String jsonString = JSONObject.toJSONString(param.get("record"));
        UserPlatformMessageDTO record = JSONObject.parseObject(jsonString, UserPlatformMessageDTO.class);
        if (param.get("collection") == null) {
            UserInfoVO userInfo = ParseTokenUtils.getPlatformUser(param.get("token").toString());
            if (null == userInfo) {
                return ResponseResult.fail(BizCodeEnume.TOKEN_IS_ILLEGAL);
            }
            record.setCreater(userInfo.getUserRealname());
        }
        int result = this.baseMapper.insertSelective(record);
        if (result > 0) {
            return ResponseResult.success(record.getId());
        }
        return ResponseResult.fail(BizCodeEnume.ADD_ERROR);
    }

    @Override
    public ResponseResult queryPlatformMessage(Map<String, Object> param) {
        String token = param.get("token").toString();

        String jsonString = RedisUtil.get(YFConfig.YF_USER_TOKEN_USER + token);
        if (StringUtils.isEmpty(jsonString)) {
            return ResponseResult.fail(BizCodeEnume.TOKEN_IS_INVALID);
        }
        UserInfoDTO userInfo = JSONObject.parseObject(jsonString, UserInfoDTO.class);

        IPage<UserPlatformMessageEntity> page = this.baseMapper.selectPage(new Query<UserPlatformMessageEntity>().getPage(param),
                new LambdaQueryWrapper<UserPlatformMessageEntity>()
                        .eq(UserPlatformMessageEntity::getUserId, userInfo.getUser().getUserId())
                        .last("order by is_reader,create_time desc"));

        return ResponseResult.success(PageUtils.getPage(page));
    }

    @Override
    public ResponseResult readPlatformMessage(Map<String, Object> param) {
        String token = (String) param.get("token");
        String jsonString = RedisUtil.get(YFConfig.YF_USER_TOKEN_USER + token);
        if (StringUtils.isEmpty(jsonString)) {
            return ResponseResult.fail(BizCodeEnume.TOKEN_IS_INVALID);
        }

        List<String> platformMessageIds = (List<String>) param.get("platformMessageIds");
        if (null == platformMessageIds || platformMessageIds.size() == 0) {
            return ResponseResult.fail(BizCodeEnume.MISSING_PARAMETER);
        }
        for (String platformMessageId : platformMessageIds) {
            UserPlatformMessageEntity record = this.baseMapper.selectById(Long.parseLong(platformMessageId));
            // 判断信息有没有阅读，==0 是没有阅读,需要更新
            if (record.getIsReader() == 0) {
                // 判断是用户 还是平台管理员，平台管理员 不能更新阅读状态
                if (StringUtils.isNotEmpty(jsonString)) {
                    UserInfoDTO userInfo = JSONObject.parseObject(jsonString, UserInfoDTO.class);
                    record.setUpdater(userInfo.getUser().getUserName());
                    record.setIsReader(1);
                    this.baseMapper.update(null, new LambdaUpdateWrapper<UserPlatformMessageEntity>()
                            .eq(UserPlatformMessageEntity::getId, record.getId())
                            .set(UserPlatformMessageEntity::getUpdater, userInfo.getUser().getUserName())
                            .set(UserPlatformMessageEntity::getIsReader, CommonType.commonStatus.YES.getCode()));

                }
            }
        }
        return ResponseResult.success();
    }

    @Override
    public ResponseResult deletePlatformMessage(Map<String, Object> param) {
        String token = (String) param.get("token");
        String jsonString = RedisUtil.get(YFConfig.YF_USER_TOKEN_USER + token);
        if (StringUtils.isEmpty(jsonString)) {
            return ResponseResult.fail(BizCodeEnume.TOKEN_IS_INVALID.getCode(), BizCodeEnume.TOKEN_IS_INVALID.getMsg());
        }

        List<String> platformMessageIds = (List<String>) param.get("platformMessageIds");
        if (null == platformMessageIds || platformMessageIds.size() == 0) {
            return ResponseResult.fail(BizCodeEnume.MISSING_PARAMETER);
        }
        for (String platformMessageId : platformMessageIds) {
            this.baseMapper.deleteById(Long.parseLong(platformMessageId));
        }
        return ResponseResult.success();
    }
}




